from PyQt5.QtCore import QTimer, QThread, pyqtSignal, QMutex


mutex = QMutex()
class Thread(QThread):
    stop_signal = pyqtSignal()

    def __init__(self, func, stop_func=None):
        self.func = func
        self.stop_func = stop_func
        super(Thread, self).__init__()
        if stop_func:
            self.stop_signal.connect(self.stop_func)

    def run(self):
        mutex.lock()
        self.func()
        mutex.unlock()


class Timer(QTimer):
    def __init__(self, func, sleep_time):
        self.sleep_time = sleep_time
        super(Timer, self).__init__()
        self.thread = Thread(func)
        self.timeout.connect(self.thread.start)

    def start(self):
        super(Timer, self).start(self.sleep_time)

